##########
#Playing to the Gallery: Emotive Rhetoric in Parliaments
#Moritz Osnabruegge, Sara B. Hobolt, Toni Rodon
#This script produces Figures 2 and A3-A10
##########


rm(list=ls())

library("ggplot2")
library("plyr")
library("gdata")
library("stringr")
library("data.table")

#Specify the path here
setwd("")

data = fread("uk_data.csv", encoding="UTF-8")

data$date = as.Date(data$date)


#Table 2: Examples: Emotive and neutral speeches
example1 = subset(data, id_speech==854597)
example1$emotive_rhetoric
example1$text

example2 = subset(data, id_speech==778143)
example2$emotive_rhetoric
example2$text

#Create time variable
data$time= NA
data$time[data$date>=as.Date("2001-01-01") & data$date<=as.Date("2001-06-30")] = "01/1"
data$time[data$date>=as.Date("2001-07-01") & data$date<=as.Date("2001-12-31")] = "01/2"
data$time[data$date>=as.Date("2002-01-01") & data$date<=as.Date("2002-06-30")] = "02/1"
data$time[data$date>=as.Date("2002-07-01") & data$date<=as.Date("2002-12-31")] = "02/2"
data$time[data$date>=as.Date("2003-01-01") & data$date<=as.Date("2003-06-30")] = "03/1"
data$time[data$date>=as.Date("2003-07-01") & data$date<=as.Date("2003-12-31")] = "03/2"
data$time[data$date>=as.Date("2004-01-01") & data$date<=as.Date("2004-06-30")] = "04/1"
data$time[data$date>=as.Date("2004-07-01") & data$date<=as.Date("2004-12-31")] = "04/2"
data$time[data$date>=as.Date("2005-01-01") & data$date<=as.Date("2005-06-30")] = "05/1"
data$time[data$date>=as.Date("2005-07-01") & data$date<=as.Date("2005-12-31")] = "05/2"
data$time[data$date>=as.Date("2006-01-01") & data$date<=as.Date("2006-06-30")] = "06/1"
data$time[data$date>=as.Date("2006-07-01") & data$date<=as.Date("2006-12-31")] = "06/2"
data$time[data$date>=as.Date("2007-01-01") & data$date<=as.Date("2007-06-30")] = "07/1"
data$time[data$date>=as.Date("2007-07-01") & data$date<=as.Date("2007-12-31")] = "07/2"
data$time[data$date>=as.Date("2008-01-01") & data$date<=as.Date("2008-06-30")] = "08/1"
data$time[data$date>=as.Date("2008-07-01") & data$date<=as.Date("2008-12-31")] = "08/2"
data$time[data$date>=as.Date("2009-01-01") & data$date<=as.Date("2009-06-30")] = "09/1"
data$time[data$date>=as.Date("2009-07-01") & data$date<=as.Date("2009-12-31")] = "09/2"
data$time[data$date>=as.Date("2010-01-01") & data$date<=as.Date("2010-06-30")] = "10/1"
data$time[data$date>=as.Date("2010-07-01") & data$date<=as.Date("2010-12-31")] = "10/2"
data$time[data$date>=as.Date("2011-01-01") & data$date<=as.Date("2011-06-30")] = "11/1"
data$time[data$date>=as.Date("2011-07-01") & data$date<=as.Date("2011-12-31")] = "11/2"
data$time[data$date>=as.Date("2012-01-01") & data$date<=as.Date("2012-06-30")] = "12/1"
data$time[data$date>=as.Date("2012-07-01") & data$date<=as.Date("2012-12-31")] = "12/2"
data$time[data$date>=as.Date("2013-01-01") & data$date<=as.Date("2013-06-30")] = "13/1"
data$time[data$date>=as.Date("2013-07-01") & data$date<=as.Date("2013-12-31")] = "13/2"
data$time[data$date>=as.Date("2014-01-01") & data$date<=as.Date("2014-06-30")] = "14/1"
data$time[data$date>=as.Date("2014-07-01") & data$date<=as.Date("2014-12-31")] = "14/2"
data$time[data$date>=as.Date("2015-01-01") & data$date<=as.Date("2015-06-30")] = "15/1"
data$time[data$date>=as.Date("2015-07-01") & data$date<=as.Date("2015-12-31")] = "15/2"
data$time[data$date>=as.Date("2016-01-01") & data$date<=as.Date("2016-06-30")] = "16/1"
data$time[data$date>=as.Date("2016-07-01") & data$date<=as.Date("2016-12-31")] = "16/2"
data$time[data$date>=as.Date("2017-01-01") & data$date<=as.Date("2017-06-30")] = "17/1"
data$time[data$date>=as.Date("2017-07-01") & data$date<=as.Date("2017-12-31")] = "17/2"
data$time[data$date>=as.Date("2018-01-01") & data$date<=as.Date("2018-06-30")] = "18/1"
data$time[data$date>=as.Date("2018-07-01") & data$date<=as.Date("2018-12-31")] = "18/2"
data$time[data$date>=as.Date("2019-01-01") & data$date<=as.Date("2019-06-30")] = "19/1"
data$time[data$date>=as.Date("2019-07-01") & data$date<=as.Date("2019-12-31")] = "19/2"

data$time2 = data$time
data$time2 = str_replace(data$time2, "/", "_")

data$stage = 0
data$stage[data$m_questions==1]= 1
data$stage[data$u_questions==1]= 2
data$stage[data$queen_debate_others==1]= 3
data$stage[data$queen_debate_day1==1]= 4
data$stage[data$pm_questions==1]= 5

calc_mean = function(x){
  m1 = mean(x$emotive_rhetoric, na.rm=T)
  m2 = mean(x$anew_rescaled, na.rm=T)
  m3 = mean(x$emotive_words, na.rm=T)
  m4 = mean(x$emotive_rhetoric_log, na.rm=T)
  m5 = mean(x$emotive_rhetoric_liwc, na.rm=T)
  m6 = mean(x$emotive_rhetoric_250_8, na.rm=T)
  m7 = mean(x$emotive_rhetoric_300_10, na.rm=T)
  m8 = mean(x$emotive_positive, na.rm=T)
  m9 = mean(x$emotive_negative, na.rm=T)
  out = cbind(m1,m2,m3,m4,m5,m6,m7,m8,m9)
}
  
result = ddply(data, c("time", "stage"), calc_mean)
colnames(result) = c("time","stage", "emotive_rhetoric", "anew_rescaled","emotive_words", "emotive_rhetoric_log", "emotive_rhetoric_liwc", "emotive_rhetoric_250_8", "emotive_rhetoric_300_10", "emotive_positive", "emotive_negative")

result$stage = as.numeric(result$stage)
result$type = NA
result$type[result$stage==5] = "PM's Question Time"
result$type[result$stage==4] = "Queen's Speech Debate: Opening Day"
result$type[result$stage==3] = "Queen's Speech Debate: Other Days"
result$type[result$stage==2] = "Urgent Questions"
result$type[result$stage==1] = "Other Question Time"
result$type[result$stage==0] = "Other"
result$time = as.character(result$time)

result$type = factor(result$type, levels = c("PM's Question Time","Queen's Speech Debate: Opening Day","Queen's Speech Debate: Other Days", "Urgent Questions", "Other Question Time",  "Other"))

result = subset(result, time!="01/1")

color_group1 = c("red4", "red4", "red4","springgreen3", "springgreen3", "springgreen3")
color_group2 = c("red", "orange", "gold","darkgreen", "blue", "black")

line_type = c(1,2,4,1,2,4)


#Figure 2: Emotive rhetoric by stage of debate over time
p1 = ggplot(data=result, aes(x=time, y=emotive_rhetoric, group=type)) + 
     geom_line(aes(color=type, linetype=type), lwd=0.65) + geom_point() + 
     xlab("Time") + ylab("Emotive Rhetoric") + 
     geom_vline(xintercept=4, linetype="dotted") +  
     geom_vline(xintercept=30, linetype="dotted") + 
     scale_y_continuous(limits=c(-3,6), breaks=seq(from=-3, to=6, by=1)) +  
     scale_colour_manual(values=color_group1) + 
     scale_linetype_manual(values=line_type) + 
     theme_bw() + theme(panel.grid.major=element_blank(), 
     panel.grid.minor=element_blank(), legend.title=element_blank(), 
     axis.line=element_line(color="black"), 
     axis.text=element_text(colour="black"),legend.text=element_text(size=11), 
     axis.text.x=element_text(size=9), axis.text.y=element_text(size=9),  
     axis.title.x=element_text(size=14), axis.title.y=element_text(size=14),
     legend.key.size=grid::unit(2.5, "lines"), legend.position="bottom")

pdf("figure_2.pdf",width=13,height=8)
print(p1)
dev.off()


#Figure A3: Emotive rhetoric by stage of debate (standard ANEW dictionary)
p2 = ggplot(data=result, aes(x=time, y=anew_rescaled, group=type)) + 
     geom_line(aes(color=type)) + geom_point() + xlab("Time") + 
     ylab("Emotive Rhetoric") + geom_vline(xintercept=4, linetype="dotted") +  
     geom_vline(xintercept=30, linetype="dotted") + 
     scale_y_continuous(limits=c(1.27,1.53)) +  
     scale_colour_manual(values=color_group2) +
     theme(text=element_text(size=12), legend.title=element_blank()) 

pdf("figure_a3.pdf",width=15,height=8)
print(p2)
dev.off()


#Figure A4: Emotive rhetoric by stage of debate (only emotive words)
p3 = ggplot(data=result, aes(x=time, y=emotive_words, group=type)) + 
     geom_line(aes(color=type)) + geom_point() + xlab("Time") + 
     ylab("Emotive Rhetoric") + geom_vline(xintercept=4, linetype="dotted") +  
     geom_vline(xintercept=30, linetype="dotted") + scale_y_continuous(limits=c(6,13)) +  
     scale_colour_manual(values=color_group2) +
     theme(text=element_text(size=12),legend.title=element_blank()) 

pdf("figure_a4.pdf",width=15,height=8)
print(p3)
dev.off()


#Figure A5: Emotive rhetoric by type of debate (with Lowe et al. scale)
p4 = ggplot(data=result, aes(x=time, y=emotive_rhetoric_log, group=type)) + 
     geom_line(aes(color=type)) + geom_point() + xlab("Time") + 
     ylab("Emotive Rhetoric") + geom_vline(xintercept=4, linetype="dotted") +  
     geom_vline(xintercept=30, linetype="dotted") + 
     scale_y_continuous(limits=c(-0.4,0.6)) +  
     scale_colour_manual(values=color_group2) +
     theme(text=element_text(size=12), legend.title=element_blank()) 

pdf("figure_a5.pdf",width=15,height=8)
print(p4)
dev.off()


#Figure A6: Emotive rhetoric by type of debate (with LIWC dictionary)
p5 = ggplot(data=result, aes(x=time, y=emotive_rhetoric_liwc, group=type)) + 
     geom_line(aes(color=type)) + geom_point() + xlab("Time") + 
     ylab("Emotive Rhetoric") + geom_vline(xintercept=4, linetype="dotted") + 
     geom_vline(xintercept=30, linetype="dotted") + 
     scale_y_continuous(limits=c(3,6)) + 
     scale_colour_manual(values=color_group2) +
     theme(text=element_text(size=12), legend.title=element_blank()) 

pdf("figure_a6.pdf",width=15,height=8)
print(p5)
dev.off()


#Figure A7: Emotive rhetoric by type of debate (dimensionality: 300, window size: 10)
p6 = ggplot(data=result, aes(x=time, y=emotive_rhetoric_300_10, group=type)) + 
     geom_line(aes(color=type)) + geom_point() + 
     xlab("Time") + ylab("Emotive Rhetoric") + 
     geom_vline(xintercept=4, linetype="dotted") +  
     geom_vline(xintercept=30, linetype="dotted") +
     scale_y_continuous(limits=c(-3,6)) + 
     scale_colour_manual(values=color_group2) +
     theme(text=element_text(size=12), legend.title=element_blank())  

pdf("figure_a7.pdf",width=15,height=8)
print(p6)
dev.off()


#Figure A8: Emotive rhetoric by type of debate (dimensionality 250, window size: 8)
p7 = ggplot(data=result, aes(x=time, y=emotive_rhetoric_250_8, group=type)) + 
     geom_line(aes(color=type)) + geom_point() + 
     xlab("Time") + ylab("Emotive Rhetoric") + 
     geom_vline(xintercept=4, linetype="dotted") + 
     geom_vline(xintercept=30, linetype="dotted") +
     scale_y_continuous(limits=c(-3.5,5)) + 
     scale_colour_manual(values=color_group2) +
     theme(text=element_text(size=12), legend.title=element_blank()) 

pdf("figure_a8.pdf",width=15,height=8)
print(p7)
dev.off()


#Figure A9: Positive emotions by type of debate
p8 = ggplot(data=result, aes(x=time, y=emotive_positive, group=type)) + 
     geom_line(aes(color=type)) + geom_point() + 
     xlab("Time") + ylab("Emotive Rhetoric") + 
     geom_vline(xintercept=4, linetype="dotted") +
     geom_vline(xintercept=30, linetype="dotted") +
     scale_y_continuous(limits=c(2,7)) +
     scale_colour_manual(values=color_group2) +
     theme(text=element_text(size=12), legend.title=element_blank()) 

pdf("figure_a9.pdf",width=15,height=8)
print(p8)
dev.off()


#Figure A10: emotive negative
p9 = ggplot(data=result, aes(x=time, y=emotive_negative, group=type)) + 
     geom_line(aes(color=type)) + geom_point() + xlab("Time") + 
     ylab("Emotive Rhetoric") + geom_vline(xintercept=4, linetype="dotted") +  
     geom_vline(xintercept=30, linetype="dotted") + 
     scale_y_continuous(limits=c(0.5,2)) +  
     scale_colour_manual(values=color_group2) +
     theme(text=element_text(size=12), legend.title=element_blank()) 

pdf("figure_a10.pdf",width=15,height=8)
print(p9)
dev.off()